package cn.kow.kmall.controller;

import cn.kow.kmall.core.response.KmallResult;
import cn.kow.kmall.pojo.entity.ItemCategory;
import cn.kow.kmall.pojo.entity.ItemCategoryGroup;
import cn.kow.kmall.pojo.entity.ItemCategoryImage;
import cn.kow.kmall.pojo.vo.FirstCategoryVO;
import cn.kow.kmall.pojo.vo.ItemCategoryVO;
import cn.kow.kmall.service.ItemCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lijun
 * @Description:
 * @Date: Created in 2019-05-13 15:27
 * @Modified By:
 */
@Slf4j
@RestController
@RequestMapping("/item/category")
public class ItemCategoryController {

    @Autowired
    private ItemCategoryService itemCategoryService;

    /**
     * 获取所有类目
     * @return 结果
     */
    @GetMapping(value = "/list")
    public KmallResult firstCategory() {
        // 所有的一级类目
        List<ItemCategory> superCategoryList = itemCategoryService.listByParentId(0L);

        List<FirstCategoryVO> categoryList = new ArrayList<>();
        for(ItemCategory category : superCategoryList) {
            FirstCategoryVO firstCategory = getFirsCategoryVO(category);
            List<ItemCategoryImage> categoryImageList = itemCategoryService.listCategoryImage(category.getId());
            firstCategory.setFocusBannerList(categoryImageList.stream()
                    .map(a -> firstCategory.new FocusBannerVO(0L, a.getPic(), a.getLinkUrl())).collect(Collectors.toList()));
            categoryList.add(firstCategory);
        }
        return KmallResult.success().put("categoryList", categoryList);
    }

    /**
     * 获取子类目
     * @return 结果
     */
    @GetMapping(value = "/sub")
    public KmallResult getCategoryList(@RequestParam Long categoryId) {
        ItemCategory category = itemCategoryService.getById(categoryId);
        FirstCategoryVO firstCategory = getFirsCategoryVO(category);
        List<ItemCategoryImage> categoryImageList = itemCategoryService.listCategoryImage(category.getId());
        firstCategory.setFocusBannerList(categoryImageList.stream()
                .map(a -> firstCategory.new FocusBannerVO(0L, a.getPic(), a.getLinkUrl())).collect(Collectors.toList()));
        return KmallResult.success().put("category", firstCategory);
    }

    private FirstCategoryVO getFirsCategoryVO(ItemCategory category) {
        FirstCategoryVO firstCategory = new FirstCategoryVO();
        firstCategory.setId(category.getId());
        firstCategory.setName(category.getName());
        firstCategory.setFrontName(category.getFrontName());
        firstCategory.setType(1);

        List<ItemCategory> subCategoryList = itemCategoryService.listByParentId(category.getId());
        Map<Long, List<ItemCategory>> categoryGroupMap = subCategoryList.stream().collect(Collectors.groupingBy(ItemCategory::getGroupId));
        List<Long> groupIdList = new ArrayList<>(categoryGroupMap.keySet());
        List<ItemCategoryGroup> categoryGroupList = itemCategoryService.listCategoryGroup(groupIdList);
        List<FirstCategoryVO.CategoryGroupVO> groupVOList = new ArrayList<>();
        for (ItemCategoryGroup group : categoryGroupList) {
            FirstCategoryVO.CategoryGroupVO groupVO = firstCategory.new CategoryGroupVO();
            groupVO.setId(group.getId());
            groupVO.setName(group.getName());
            groupVO.setCategoryList(toItemCategoryVOList(categoryGroupMap.get(group.getId())));
            groupVOList.add(groupVO);
        }
        if(categoryGroupMap.containsKey(0L)) {
            FirstCategoryVO.CategoryGroupVO groupVO = firstCategory.new CategoryGroupVO();
            groupVO.setId(0L);
            groupVO.setName("");
            groupVO.setCategoryList(toItemCategoryVOList(categoryGroupMap.get(0L)));
            groupVOList.add(0, groupVO);
        }
        firstCategory.setCategoryGroupList(groupVOList);
        firstCategory.setSubCategoryList(toItemCategoryVOList(subCategoryList));
        return firstCategory;
    }

    private List<ItemCategoryVO> listBySuperId(Long superId) {
        List<ItemCategory> categoryList = itemCategoryService.listByParentId(superId);
        List<ItemCategoryVO> voList = categoryList.stream()
                .map(itemCategory -> toItemCategoryVO(itemCategory))
                .collect(Collectors.toList());
        return voList;
    }

    List<ItemCategoryVO> toItemCategoryVOList(List<ItemCategory> categoryList) {
        List<ItemCategoryVO> voList = categoryList.stream()
                .map(itemCategory -> toItemCategoryVO(itemCategory))
                .collect(Collectors.toList());
        return voList;
    }

    ItemCategoryVO toItemCategoryVO(ItemCategory itemCategory) {
        return new ItemCategoryVO(itemCategory.getId(), itemCategory.getPid(), itemCategory.getName(), itemCategory.getFrontName(), itemCategory.getPic());
    }
}
